#!/bin/bash

set +e
set -x

export TIME_ID=`date +%s`

# 变更部署id

sed -i "s#BUILD_BUILD_ENV_ID#$TIME_ID#g" /usr/share/nginx/html/packages/microConfig.json
sed -i "s#BUILD_BUILD_ENV_ID#$TIME_ID#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_PACKAGE_PUBLIC_URL#$BUILD_PACKAGE_PUBLIC_URL#g" /usr/share/nginx/html/index.html
sed -i "s#/BUILD_PUBLIC_URL/#${BUILD_PUBLIC_URL:-/}#g" /usr/share/nginx/html/index.html
sed -i "s#/BUILD_BASE_PATH/#${BUILD_BASE_PATH:-/}#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_API_HOST#$BUILD_API_HOST#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_ENV_NAME#$BUILD_ENV_NAME#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_PLATFORM_VERSION#$BUILD_PLATFORM_VERSION#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_WEBSOCKET_HOST#$BUILD_WEBSOCKET_HOST#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_CLIENT_ID#$BUILD_CLIENT_ID#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_BAIDU_MAP_AK#$BUILD_BAIDU_MAP_AK#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_HISTORY_ENABLE#$BUILD_HISTORY_ENABLE#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_TOP_MENU_LABELS#$BUILD_TOP_MENU_LABELS#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_TOP_MENU_UNION_LABEL#$BUILD_TOP_MENU_UNION_LABEL#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_IM_WEBSOCKET_HOST#$BUILD_IM_WEBSOCKET_HOST#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_DGC_WEBSOCKET_HOST#$BUILD_DGC_WEBSOCKET_HOST#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_AIGC_HOST#$BUILD_AIGC_HOST#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_AIGC_WEBSOCKET_HOST#$BUILD_AIGC_WEBSOCKET_HOST#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_AIGC_STORAGE_CODE#$BUILD_AIGC_STORAGE_CODE#g" /usr/share/nginx/html/index.html
sed -i "s#BUILD_CUSTOMIZE_ICON_NAME#$BUILD_CUSTOMIZE_ICON_NAME#g" /usr/share/nginx/html/index.html

# 检查变量是否存在且不以 '/' 开头
if [ -n "$BUILD_PUBLIC_URL" ] && [ "${BUILD_PUBLIC_URL:0:1}" != "/" ]; then
  sed -i "s|<script src=\"/|<script src=\"|g" /usr/share/nginx/html/index.html
else
  sed -i "s|<script src=\"/|<script src=\"${BUILD_PUBLIC_URL:-/}|g" /usr/share/nginx/html/index.html
fi

# 如果图片 hzero-ui icon 无法访问外网字体文件， 需要注释掉下面的注释
# find /usr/share/nginx/html/packages/hzero_front_components -name '*.css' | xargs sed -i "s#/BUILD_PUBLIC_URL/#${BUILD_PUBLIC_URL:-/}#g" || echo replace error

echo 'replace complete!'
